package com.project.weimi.widget;


import com.project.weimi.R;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ToastMessage {
	private static Toast imageToast = null;
	
	private static String oldMsg;
	protected static Toast toast = null;
	private static long oneTime = 0;
	private static long twoTime = 0;
	
	/**
	 * 第一种吐丝方法
	 * @param context
	 * @param s
	 */
	public static void showToast(Context context, String s) {
		if (toast == null) {
			toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
			toast.show();
			oneTime = System.currentTimeMillis();
		} else {
			twoTime = System.currentTimeMillis();
			if (s.equals(oldMsg)) {
				if (twoTime - oneTime > Toast.LENGTH_SHORT) {
					toast.show();
				}
			} else {
				oldMsg = s;
				toast.setText(s);
				toast.show();
			}
		}
		oneTime = twoTime;
	}
	
	/**
	 * 第二种自定义吐丝方法
	 * @param context
	 * @param msg
	 */
	@SuppressLint("NewApi")
	public static void showMyToast(Context context,String msg) {
		if (imageToast == null) {
			imageToast =new Toast(context);
			LinearLayout toastLayout = new LinearLayout(context);
			LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			toastLayout.setOrientation(LinearLayout.HORIZONTAL);
			TextView textView = new TextView(context);
			textView.setText(msg);
			textView.setTextColor(Color.parseColor("#ffffff"));
			textView.setBackgroundResource(R.drawable.promptmessage_bg);
			textView.setGravity(Gravity.CENTER);
			toastLayout.addView(textView, lp);
			imageToast.setView(toastLayout);
			imageToast.setDuration(Toast.LENGTH_SHORT);
			imageToast.setView(toastLayout);
			imageToast.setGravity(Gravity.CENTER, 0, 0);
        } else {
        	//imageToast.cancel();//关闭吐司显示
        	LinearLayout toastLayout = new LinearLayout(context);
    		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
    				LayoutParams.WRAP_CONTENT);
    		toastLayout.setOrientation(LinearLayout.HORIZONTAL);
    		TextView textView = new TextView(context);
    		textView.setText(msg);
    		textView.setTextColor(Color.parseColor("#ffffff"));
    		textView.setBackgroundResource(R.drawable.promptmessage_bg);
    		textView.setGravity(Gravity.CENTER);
    		toastLayout.addView(textView, lp);
    		imageToast.setView(toastLayout);
    		imageToast.setDuration(Toast.LENGTH_SHORT);
    		imageToast.setView(toastLayout);
    		imageToast.setGravity(Gravity.CENTER, 0, 0); 
        }
		imageToast.show(); //重新显示吐司
	}

}
